package com.fgs.common.imageView.strategy.impl;

import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.fgs.common.R;
import com.fgs.common.api.CommonConfig;
import com.fgs.common.imageView.strategy.IImageLoaderStrategy;
import com.fgs.common.manager.CommonSharedPreferenceManager;
import com.fgs.common.utils.LogUtil;

/**
 * @author fgs
 * @date 2020/5/12
 * @description
 **/
public class GlideImageLoaderStrategy implements IImageLoaderStrategy {

    @Override
    public void loadImageWithId(ImageView imageView, String imageId) {
////        if (TextUtils.isEmpty(imageId)) return;
//        String url = CommonConfig.getInstance().commonOptions().getBaseImageUrl() + imageId + ".jpg";
//        Glide.with(imageView.getContext())
//                .load(url)
//                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .placeholder(R.mipmap.icon_image_loading)
//                .into(imageView);

        loadImageWithId(imageView, imageId, true);
    }

    @Override
    public void loadImageWithIdAndSuffix(ImageView imageView, String imageId) {
        loadImageWithId(imageView, imageId + ".jpg", false);
    }

    @Override
    public void loadImageWithId(ImageView imageView, String imageId, boolean isNeedCache) {
        DiskCacheStrategy diskCacheStrategy = DiskCacheStrategy.NONE;
        if (isNeedCache) {
            diskCacheStrategy = DiskCacheStrategy.ALL;
        }

        String url = CommonConfig.getInstance().commonOptions().getBaseImageUrl() + imageId;

        Glide.with(imageView.getContext())
                .load(getGlideUrl(url))
                .skipMemoryCache(!isNeedCache)
                .diskCacheStrategy(diskCacheStrategy)
                .placeholder(R.mipmap.icon_image_loading)
                .into(imageView);
    }

    @Override
    public void loadImageWithUrl(ImageView imageView, String url, boolean isNeedCache) {
        if (TextUtils.isEmpty(url)) return;
        LogUtil.print(url);
        DiskCacheStrategy diskCacheStrategy = DiskCacheStrategy.NONE;
        if (isNeedCache) {
            diskCacheStrategy = DiskCacheStrategy.ALL;
        }
        Glide.with(imageView.getContext())
                .load(url)
                .skipMemoryCache(!isNeedCache)
                .diskCacheStrategy(diskCacheStrategy)
                .placeholder(R.mipmap.icon_image_loading)
                .into(imageView);
    }

    @Override
    public void loadImageWithUrl(ImageView imageView, String url, int defaultImage) {
        if (TextUtils.isEmpty(url)) return;
        Glide.with(imageView.getContext())
                .load(url)
//                .skipMemoryCache(true)
//                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(defaultImage)
                .into(imageView);
    }

    private GlideUrl getGlideUrl(String url) {
        String token = CommonSharedPreferenceManager.getInstance().getToken();
        GlideUrl glideUrl = new GlideUrl(url, new LazyHeaders.Builder().addHeader("Authorization", token).build());
        return glideUrl;

    }
}
